import React from "react";
import {
  text_check,
  logoImg,
  encryption,
  urlQuery,
  post_axios,
  msg_alert,
} from "../../utils";
import { useTranslation } from "react-i18next";
import cookie from "react-cookies";
import { Input, Image, message,Spin  } from "antd";
import sha1 from "js-sha1";

export default function LoginBox(props) {
  const { t } = useTranslation();

  const {
    username,
    password,
    setUsername,
    setPassword,
    setChangePwdShow,
    setCheckIdentityShow,
    login_success_func,
    setEmail,
  } = props;

  const params_obj = urlQuery(window.location.href);
  const company = params_obj["u"];
  const [spinning, setSpinning] = React.useState(false);

  const usernameIpt = (value) => {
    const { value: username } = value.currentTarget;
    setUsername(username);
  };
  const passwordIpt = (value) => {
    const { value: pwd } = value.currentTarget;
    setPassword(pwd);
  };

  const login_fuc = async () => {
    if (username === "") {
      message.warning({
        content: t("login.enter_username"),
      });
      return;
    }
    if (password === "") {
      message.warning({
        content: t("login.enter_pwd"),
      });
      return;
    }
    let reg = new RegExp("[\\u4E00-\\u9FFF]+", "g");
    if (reg.test(username) || reg.test(password)) {
      message.warning({
        content: t("system_manager.no_chinese"),
      });
      return;
    }
    setSpinning(true);
    let upMsg = {
      user: username,
      pwd: encryption(sha1(password)),
      cookie: cookie.load("login_timer") ? cookie.load("login_timer") : "",
    };
    let res = await post_axios("login.web", company, upMsg);
    setSpinning(false);
    if (!res) return;
    if (res.data && res.data.session_id) {
      sessionStorage.setItem("session_id", res.data.session_id);
    }
    switch (res.flag) {
      case "0000":
        //登陆成功
        login_success_func(res);
        break;
      case "1108":
        //需要验证身份
        setEmail(res.data.email);
        setCheckIdentityShow(true);
        // code_btn_word.value = t("login.get_code");
        // check_identity_code.value = "";
        // check_identity_userValue = res.data.tel ? res.data.tel : res.data.email;
        // check_identity_show.value = true;
        break;
      case "1114":
        setChangePwdShow(true);
        break;

      default:
        let data =
          res.data && res.data !== null ? JSON.stringify(res.data.data) : "";
        let msg = company === "mg" ? res.msg_en : res.msg_cn;
        msg_alert(res.flag, msg, data);
    }
  };

  return (
    <>
      <div className="login_box">
        <div className="system_logo">
          <Image height="50px" src={logoImg()} preview={false} fit="contain" />
        </div>
        <div className="system_name">{text_check()}</div>
        <div className="system_login_input">
          <Input
            onChange={usernameIpt}
            onPressEnter={login_fuc}
            className="name_input_style"
            placeholder={t("login.login_enter_username")}
          />
          <Input.Password
            onChange={passwordIpt}
            onPressEnter={login_fuc}
            className="pwd_input_style"
            placeholder={t("login.enter_pwd")}
          />
        </div>

        <div className="login_btn">
          <Image
            height="40px"
            width="40px"
            src="images/next.png"
            fit="contain"
            preview={false}
            onClick={login_fuc}
          />
        </div>
      </div>
      <Spin spinning={spinning} fullscreen />
    </>
  );
}
